package com.sgb.capital.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.capital.R;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.MyZTextWatcher;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.capital.databinding.TransferaccountsActivityBinding;
import com.sgb.capital.model.Bean;
import com.sgb.capital.model.TransferBean;
import com.sgb.capital.utils.AdapterOnItemClick;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.pop.SelectPop;
import com.sgb.capital.view.ui.adapter.FinanceFlowItemAdapter;
import com.sgb.capital.view.ui.adapter.TransferAccountsAdapter;
import com.sgb.capital.view.widget.xrecyclerview.ZXRecyclerView;
import com.sgb.capital.viewmodel.DealLimitModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

/**
 * 作者:张磊
 * 日期:2021/10/9 0009
 * 说明:转账列表
 */
public class TransferAccountsActivity extends BaseActivity {
    private TransferaccountsActivityBinding mBinding;
    private DealLimitModel mModel;
    private TransferAccountsAdapter mAdapter;
    private FinanceFlowItemAdapter mFlowItemAdapter;
    private SelectPop mSelectPop;
    private TextView mTextView;
    private ImageView mImg;
    private List<Bean> mType;
    private List<Bean> mApproveType;
    private List<Bean> mTimes;
    private int mIndex = 1;
    private HashMap mMap;

    public static void start(Context context) {
        Intent intent = new Intent(context, TransferAccountsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        mBinding = DataBindingUtil.setContentView(this, R.layout.transferaccounts_activity);
        mModel = new ViewModelProvider(this).get(DealLimitModel.class);
        // 选项
        mBinding.rvItem.setLayoutManager(new GridLayoutManager(Utils.getContext(), 3));
        mFlowItemAdapter = new FinanceFlowItemAdapter(null);
        mBinding.rvItem.setAdapter(mFlowItemAdapter);
        // 列表
        mAdapter = new TransferAccountsAdapter(this, null);
        mBinding.rvList.setLayoutManager(new GridLayoutManager(Utils.getContext(), 1));
        mBinding.rvList.setAdapter(mAdapter);
        // 筛选框
        mSelectPop = new SelectPop(this);
        mSelectPop.setBgView(mBinding.grayLayout);
    }

    public void initData() {
        mType = Utils.setDatas(new String[]{"全部", "B2B网银", "余额支付"});
        mTimes = Utils.setDatas(new String[]{"全部", "今日", "最近一周", "最近一个月", "最近三个月", "最近六个月", "最近一年", "最近三年"});
        mApproveType = Utils.setDatas(new String[]{"全部", "审批中", "已通过", "已拒绝"});
        List<Bean> beans = new ArrayList<>();
        beans.add(new Bean("转账方式"));
        beans.add(new Bean("转账时间"));
        beans.add(new Bean("审批状态"));
        mFlowItemAdapter.setNewInstance(beans);
        mMap = new HashMap();
        mMap.put("limit", 20);
        mMap.put("page", mIndex);
        mModel.getTransferInfoList(this, mMap);
    }

    public void initListener() {
        mBinding.tvRight.setOnClickListener(v -> {
            AddTransferAccountsActivity.start(this);
        });
        mBinding.etSs.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mIndex = 1;
                mMap.put("page", mIndex);
                String trim = mBinding.etSs.getText().toString().trim();
                try {
                    Double.parseDouble(trim);
                    mMap.put("orderId", trim);
                } catch (Exception e) {
                    mMap.put("payeeCompanyName", trim);
                }
                mModel.getTransferInfoList(TransferAccountsActivity.this, mMap);
            }
            return false;
        });
        mBinding.etSs.addTextChangedListener(new MyZTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String editable = mBinding.etSs.getText().toString();
                String str = Utils.stringFilter(mBinding.etSs.getText().toString());
                if (!editable.equals(str)) {
                    mBinding.etSs.setText(str);
                    mBinding.etSs.setSelection(str.length());
                }
            }
        });
        mBinding.rvList.setLoadingListener(new ZXRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mIndex = 1;
                mMap.put("page", mIndex);
                mModel.getTransferInfoList(TransferAccountsActivity.this, mMap);
            }

            @Override
            public void onLoadMore() {
                mIndex++;
                mMap.put("page", mIndex);
                mModel.getTransferInfoList(TransferAccountsActivity.this, mMap);
            }
        });

        mAdapter.setOnItemClick(new AdapterOnItemClick<TransferBean.ListDTO>() {
            @Override
            public void onItemClick(TransferBean.ListDTO listDTO, int position) {
                TransferAccountsDetailsActivity.start(TransferAccountsActivity.this, listDTO.id);
            }
        });
        mBinding.ivBack.setOnClickListener(v -> finish());
        mFlowItemAdapter.setOnItemClickListener((adapter, view, position) -> {
            LinearLayout linearLayout = (LinearLayout) view;
            mTextView = (TextView) linearLayout.getChildAt(0);
            mImg = (ImageView) linearLayout.getChildAt(1);
            if (mTextView.getText().toString().contains("转账") || mTextView.getText().toString().contains("状态")) {
                mTextView.setTextColor(Utils.getColor(R.color.color_333333));
            }
            mImg.setRotation(180);
            switch (position) {
                case 0:
                    mSelectPop.mType = 1;
                    mSelectPop.setData(mType);
                    mSelectPop.mAdapter.setDefItem(mSelectPop.mPos1);
                    break;
                case 1:
                    mSelectPop.mType = 2;
                    mSelectPop.setData(mTimes);
                    mSelectPop.mAdapter.setDefItem(mSelectPop.mPos2);
                    break;
                case 2:
                    mSelectPop.mType = 3;
                    mSelectPop.setData(mApproveType);
                    mSelectPop.mAdapter.setDefItem(mSelectPop.mPos3);
                    break;
            }
            mSelectPop.showPopwindow(mBinding.view, 0, 0);
        });

        mSelectPop.setOnDismissListener(() -> {
            if (mSelectPop.mType == 1 && mSelectPop.mPos1 == 0) {
                mTextView.setText("转账方式");
                mMap.put("method", null);
            } else if (mSelectPop.mType == 2 && mSelectPop.mPos2 == 0) {
                mTextView.setText("转账时间");
                mMap.put("endTime", null);
                mMap.put("startTime", null);
            } else {
                if (mSelectPop.mIsClick) {
                    mTextView.setText(mSelectPop.mBean.name.contains("B2B") ? mSelectPop.mBean.name : mSelectPop.mBean.name.length() > 5 ? mSelectPop.mBean.name.substring(0, 5) + "..." : mSelectPop.mBean.name);
                    if (mSelectPop.mType == 1) {
                        mMap.put("method", mSelectPop.mBean.name.contains("B2B") ? "B2BBANK" : "BALANCE");
                    } else if (mSelectPop.mType == 2) {
                        if (mSelectPop.mPos2 == 0) {
                            mMap.put("endTime", null);
                            mMap.put("startTime", null);
                        } else {
                            String time = mSelectPop.mBean.name.contains("今日") ? Utils.getStartAndEndTime(0, 0, "今日")
                                    : mSelectPop.mBean.name.contains("最近一周") ? Utils.getStartAndEndTime(1, 7, "最近一周")
                                    : mSelectPop.mBean.name.contains("最近一个月") ? Utils.getStartAndEndTime(2, 1, "最近一个月")
                                    : mSelectPop.mBean.name.contains("最近三个月") ? Utils.getStartAndEndTime(2, 3, "最近三个月")
                                    : mSelectPop.mBean.name.contains("最近六个月") ? Utils.getStartAndEndTime(2, 6, "最近六个月")
                                    : mSelectPop.mBean.name.contains("最近一年") ? Utils.getStartAndEndTime(3, 1, "最近一年")
                                    : Utils.getStartAndEndTime(3, 3, "最近三年");
                            mMap.put("endTime", time.split("-")[1]);
                            mMap.put("startTime", time.split("-")[0]);
                        }
                    } else if (mSelectPop.mType == 3) {
                        mTextView.setText(mSelectPop.mPos3 == 0 ? "审批状态" : mSelectPop.mBean.name);
                        mMap.put("payApplyState", mSelectPop.mPos3 == 0 ? null : mSelectPop.mPos3);
                    }
                }
            }
            mImg.setRotation(0);
            if (mSelectPop.mIsClick) {
                mTextView.setTextColor(Utils.getColor(
                        mSelectPop.mType == 1 ? mSelectPop.mPos1 != 0 ? R.color.color_EF4033 : R.color.color_666666 :
                                mSelectPop.mType == 2 ? mSelectPop.mPos2 != 0 ? R.color.color_EF4033 : R.color.color_666666 :
                                        mSelectPop.mPos3 != 0 ? R.color.color_EF4033 : R.color.color_666666
                ));
                mImg.setImageResource(mSelectPop.mType == 1 ? mSelectPop.mPos1 != 0 ?
                        R.mipmap.down : R.mipmap.ic_down_arrow_n : mSelectPop.mType == 2 ?
                        mSelectPop.mPos2 != 0 ? R.mipmap.down : R.mipmap.ic_down_arrow_n :
                        mSelectPop.mPos3 != 0 ? R.mipmap.down : R.mipmap.ic_down_arrow_n
                );
                mIndex = 1;
                mMap.put("page", mIndex);
                mModel.getTransferInfoList(this, mMap);
            }
            mSelectPop.mIsClick = false;
        });


    }

    @Override
    public void initObserve() {
        mModel.mTransferBeans.observe(this, data -> {
            if (mIndex == 1) {
                mBinding.ivNull.setVisibility(data.size() != 0 ? View.GONE : View.VISIBLE);
                mBinding.rvList.setLoadingMoreEnabled(true);
                mBinding.rvList.refreshComplete();
                mAdapter.setDatas(data);
            } else {
                if (data.size() != 0) {
                    mBinding.rvList.loadMoreComplete();
                    mAdapter.addDatas(data);
                } else {
                    mBinding.rvList.setNoMore(true);
                }
            }
            mAdapter.notifyDataSetChanged();
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void getCode(ZMessageEvent event) {
        switch (event.code) {
            case Constants
                    .EVENT_HIDE_ORDERID:
                Utils.postDelay(() -> {
                    mIndex = 1;
                    mMap.put("page", mIndex);
                    mModel.getTransferInfoList(this, mMap);
                }, 500);
                break;
        }
    }
}